package com.base.demo.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.*
import com.alibaba.android.arouter.facade.annotation.Route
import com.base.common.constant.RoutePath
import com.base.common.fragment.BaseFragment
import com.base.demo.data.Login
import com.base.demo.databinding.DemoDemoFragmentBinding
import com.base.demo.viewmodel.DemoViewModel
import com.drake.net.utils.scopeNetLife
import com.kotlin.base.net.addObservice
import com.tao.data.Resource
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RoutePath.DEMO_FRAGMENT)
class DemoFragmnet() : BaseFragment<DemoDemoFragmentBinding, DemoViewModel>() {
    override val viewModel: DemoViewModel by viewModels()

    override val binding: DemoDemoFragmentBinding by lazy {
        DemoDemoFragmentBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initData() {
        super.initData()
        viewModel.getData()


        lifecycleScope.launch {
            delay(10000)
            viewModel.getData()


        }
        binding.btn2.setOnClickListener {
            viewModel.getData()
            //go()

        }
    }

    override fun initView() {
        super.initView()


    }

    override fun onResume() {
        super.onResume()
        // viewModel.getData()
    }


    override fun initObservable() {
        super.initObservable()

        go()


    }


    fun go() {

      addObservice {
          viewModel.dataDemo.collect{
              when (it) {
                  is Resource.Success -> Log.e("success", it.data.toString())
                  is Resource.Error -> Log.e("error", it.error.toString())
              }
          }
      }



    }
}